package com.itchen.service.impl;

import com.itchen.controller.R;
import com.itchen.exception.BusinessException;
import com.itchen.mapper.BrandMapper;
import com.itchen.pojo.Brand;
import com.itchen.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author げっしょく
 * @date 2021/9/8
 */

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public R insert(Brand brand) {
        if (brand.getBrandName().length() > 20 || brand.getCompanyName().length() > 20) {
            throw new BusinessException(false, "请不要用你的技术来挑战我的耐心");
        }
        int insert = brandMapper.insert(brand);
        if (insert > 0) {
            return new R(true);
        }
        return new R(false);
    }

    public R delByIdAfter(int id) {
        if (id < 0) {
            throw new BusinessException(false, "请不要用你的技术来挑战我的耐心");
        }
        int delByIdAfter = brandMapper.delByIdAfter(id);
        System.out.println(delByIdAfter);
        if (delByIdAfter > 0) {

            return new R(true, null, "删除成功");
        }
        return new R(false, null, "删除失败");
    }

    public R update(Brand brand) {
        if (brand.getBrandName().length() > 20 || brand.getCompanyName().length() > 20) {
            throw new BusinessException(false, "请不要用你的技术来挑战我的耐心");
        }
        int update = brandMapper.update(brand);
        if (update > 0) {
            return new R(true);
        }
        return new R(false);
    }

    public R selectAll() {
        List<Brand> brands = brandMapper.selectAll();
        boolean flag = brands != null ? true : false;
        return new R(false, brands);
    }

    public R delarray(int[] ids) {
        int delarray = brandMapper.delarray(ids);
        if (delarray > 0) {
            return new R(true, null, "删除成功");
        }
        return new R(false, null, "删除失败");
    }

    public R selectByPage(int index, int page) {

//        System.out.println("brandService" + index + "..." + page);

        List<Brand> brands = brandMapper.selectByPage(index, page);

        boolean flag = brands != null ? true : false;

        if (flag) {
            return new R(flag, brands);
        }
        return new R(flag, brands, "没有查询到数据");
    }

    public int selectCount() {
        int i = brandMapper.selectCount();

        return i;
    }

    public R selectByPageAndcond(int index, int page, Brand brand) {
        if (brand.getBrandName().length() > 20 || brand.getCompanyName().length() > 20) {
            throw new BusinessException(false, "请不要用你的技术来挑战我的耐心");
        }
        System.out.println(brand);
        List<Brand> brands = brandMapper.selectByPageAndcond(index, page, brand);
        boolean flag = brands != null ? true : false;
        return new R(false, brands);
    }

    public int selectCountAndcond(Brand brand) {
        int i = brandMapper.selectCountAndcond(brand);

        return i;
    }
}
